package com.njitt.bysj.srevice;

import cn.hutool.core.date.DateTime;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.njitt.bysj.base.BaseService;
import com.njitt.bysj.bean.BasicQuotationBean;
import com.njitt.bysj.bean.InventoryBean;
import com.njitt.bysj.bean.ProjectBean;
import com.njitt.bysj.bean.ProjectDesignBean;
import com.njitt.bysj.mapper.BasicQuotationBeanMapper;
import com.njitt.bysj.mapper.InventoryBeanMapper;
import com.njitt.bysj.mapper.ProjectBeanMapper;
import com.njitt.bysj.mapper.ProjectDesignBeanMapper;
import com.njitt.bysj.model.PickStockModel;
import com.njitt.bysj.query.BasicSaleQuery;
import com.njitt.bysj.query.ProjectQuery;
import com.njitt.bysj.utils.AssertUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * @description:
 * @author: wyj
 * @time: 2022/4/7 23:06
 */
@Service
public class SaleService extends BaseService<BasicQuotationBean, Integer> {

    @Resource
    private BasicQuotationBeanMapper basicQuotationMapper;

    @Resource
    private ProjectBeanMapper projectMapper;
    @Resource
    private ProjectDesignBeanMapper projectDesignBeanMapper;
    @Resource
    private InventoryBeanMapper inventoryBeanMapper;

    public Map<String, Object> basic_list(BasicSaleQuery query) {
        PageHelper.startPage(query.getPage(), query.getLimit());
        PageInfo<BasicQuotationBean> info = new PageInfo<>(basicQuotationMapper.selectByParams(query));
        Map<String, Object> map = new HashMap<>();
        map.put("code", 0);
        map.put("count", info.getTotal());
        map.put("data", info.getList());
        map.put("msg", "success");
        return map;
    }


    @SuppressWarnings("AlibabaTransactionMustHaveRollback")
    @Transactional(propagation = Propagation.REQUIRED)
    public void addBasic(BasicQuotationBean basicQuotationBean) {
        AssertUtil.isTrue(basicQuotationBean.getEmpId() == null, "登录过期");
        AssertUtil.isTrue(StringUtils.isBlank(basicQuotationBean.getCustomerName()), "客户名称不能为空");
        AssertUtil.isTrue(StringUtils.isBlank(basicQuotationBean.getProductName()), "产品名称不能为空");
        AssertUtil.isTrue(StringUtils.isBlank(basicQuotationBean.getCustomerAddress()), "地址不能为空");
        AssertUtil.isTrue(basicQuotationBean.getProductCount() == null || basicQuotationBean.getProductCount() == 0, "数量不能为空");
        AssertUtil.isTrue(basicQuotationBean.getProductUnitPrice() == null || basicQuotationBean.getProductUnitPrice() == 0, "单价不能为空");
        basicQuotationBean.setStatus("0");
        basicQuotationBean.setCreateTime(new DateTime());
        basicQuotationBean.setUpdateTime(new DateTime());
        Integer integer = basicQuotationMapper.insertHasKey(basicQuotationBean);
        AssertUtil.isTrue(integer == null || integer == 0, "添加失败");
    }


    @SuppressWarnings("AlibabaTransactionMustHaveRollback")
    @Transactional(propagation = Propagation.REQUIRED)
    public void updateDescription(Integer id, String description) {
        /**
         * @description: 文件名写入数据库
         * * @return:  * @param id
         * @param description 文件名
         * @author:
         * @time: 2022/4/10 23:16
         */
        AssertUtil.isTrue(id == null, "上传失败");
        AssertUtil.isTrue(StringUtils.isBlank(description), "文件名为空");
        BasicQuotationBean basicQuotationBean = new BasicQuotationBean();
        basicQuotationBean.setId(id);
        basicQuotationBean.setDescription(description);
        basicQuotationBean.setStatus("1");
        Integer integer = basicQuotationMapper.updateByPrimaryKeySelective(basicQuotationBean);
        AssertUtil.isTrue(integer == 0 || integer == null, "上传失败");
    }

    public String fileName(Integer id) {
        /**
         * @description: 根据id查文件名
         * @return:  * @param id
         * @author:
         * @time: 2022/4/10 23:16
         */
        AssertUtil.isTrue(id == null, "上传失败");
        return basicQuotationMapper.queryFileNameById(id);
    }


    public String queryFileNameById(Integer id) {
        AssertUtil.isTrue(id == null, "上传失败");
        String fileName = basicQuotationMapper.queryFileNameById(id);
        return fileName;
    }

    public BasicQuotationBean queryBasicByPrimaryKey(Integer id) {
        AssertUtil.isTrue(id == null, "更新失败");
        BasicQuotationBean basicQuotationBean = basicQuotationMapper.queryBasicByPrimaryKey(id);
        AssertUtil.isTrue(basicQuotationBean == null, "更新失败2");
        return basicQuotationBean;
    }

    public void updateBasic(BasicQuotationBean basicQuotationBean) {
        AssertUtil.isTrue(basicQuotationBean == null, "更新失败");
        AssertUtil.isTrue(basicQuotationBean.getId() == null, "更新失败2");
        Integer integer = basicQuotationMapper.updateByPrimaryKeySelective(basicQuotationBean);
        AssertUtil.isTrue(integer == null || Integer.parseInt(String.valueOf(integer)) == 0, "更新失败3");
    }

    public Map<String, Object> projectList(ProjectQuery projectQuery) {
        PageHelper.startPage(projectQuery.getPage(), projectQuery.getLimit());
        PageInfo<ProjectBean> info = new PageInfo<>(projectMapper.selectByParams(projectQuery));
        Map<String, Object> map = new HashMap<>(16);
        map.put("code", 0);
        map.put("count", info.getTotal());
        map.put("data", info.getList());
        map.put("msg", "success");
        return map;
    }

    public ProjectBean queryProjectById(Integer id) {
        AssertUtil.isTrue(id == null, "操作失败");
        return projectMapper.selectByPrimaryKey(id);

    }


    @SuppressWarnings("AlibabaTransactionMustHaveRollback")
    @Transactional(propagation = Propagation.REQUIRED)
    public void updateContract(Integer id, String fileName) {
        /**
         * @description: 上传合同
         * @return:  * @param id
         * @param fileName  文件名
         * @author:
         * @time: 2022/4/12 16:57
         */
        AssertUtil.isTrue(id == null, "上传失败");
        AssertUtil.isTrue(StringUtils.isBlank(fileName), "文件名为空");
        ProjectBean projectBean = new ProjectBean();
        projectBean.setStatus(1);
        projectBean.setId(id);
        projectBean.setProjectContract(fileName);
        Integer integer = projectMapper.updateByPrimaryKeySelective(projectBean);
        AssertUtil.isTrue(Integer.parseInt(String.valueOf(integer)) == 0 || integer == null, "上传失败");

    }


    public BasicQuotationBean selectByBasicId(Integer basicId) {
        AssertUtil.isTrue(basicId == null, "审批失败");
        BasicQuotationBean basicQuotationBean = basicQuotationMapper.selectByPrimaryKey(basicId);
        AssertUtil.isTrue(basicQuotationBean == null, "审批失败");
        return basicQuotationBean;
    }

    @SuppressWarnings("AlibabaTransactionMustHaveRollback")
    @Transactional(propagation = Propagation.REQUIRED)
    public void addProject(ProjectBean projectBean) {
        AssertUtil.isTrue(projectBean.getBasicId() == null, "审批失败");
        AssertUtil.isTrue(projectBean.getProjectApprovalResults() == null, "审批失败");
        BasicQuotationBean basicQuotationBean = basicQuotationMapper.selectByPrimaryKey(projectBean.getBasicId());
        System.out.println(basicQuotationBean);
        AssertUtil.isTrue(basicQuotationBean == null, "审批失败");
        projectBean.setProjectTotalAmount(basicQuotationBean.getProductCount() * basicQuotationBean.getProductUnitPrice());
        projectBean.setStatus(0);
        projectBean.setProjectCreateDate(new Date());
        projectBean.setProjectNo(UUID.randomUUID().toString().replace("-", ""));
        if (projectBean.getProjectApprovalResults() == 0) {
            basicQuotationBean.setStatus("2");
            // 加到销售项目表
            Integer integer = projectMapper.insertHasKey(projectBean);
            ProjectDesignBean designBean = new ProjectDesignBean();
            designBean.setProjectId(projectBean.getId());
            designBean.setStatus(0);
            Integer integer1 = projectDesignBeanMapper.insertSelective(designBean);
            AssertUtil.isTrue(integer == null || integer == 0, "审批失败");
            AssertUtil.isTrue(integer1 == null || integer == 0, "审批失败");
        } else if (projectBean.getProjectApprovalResults() == 1) {
            basicQuotationBean.setStatus("3");
        }
        projectBean.setProjectCreateDate(new Date());
        Integer integer1 = basicQuotationMapper.updateByPrimaryKeySelective(basicQuotationBean);
        AssertUtil.isTrue(integer1 == null || integer1 == 0, "审批失败");
    }

    public ProjectBean selectById(Integer projectId) {
        AssertUtil.isTrue(projectId == null || projectId == 0, "异常了");
        return projectMapper.selectByPrimaryKey(projectId);
    }

    @SuppressWarnings("AlibabaTransactionMustHaveRollback")
    @Transactional(propagation = Propagation.REQUIRED)
    public void addPickStock(PickStockModel pickStockModel) {
        AssertUtil.isTrue(StringUtils.isBlank(pickStockModel.getProjectNo()), "项目号为空");
        ProjectBean projectBean = projectMapper.selectProjectByProjectNo(pickStockModel.getProjectNo());
        AssertUtil.isTrue(projectBean == null, "项目不存在");
        AssertUtil.isTrue(projectBean.getStatus() != 3, "项目号错误");
        AssertUtil.isTrue(!projectBean.getBasicQuotationBean().getProductName().equals(pickStockModel.getProductName()), "产品名称有误");
        AssertUtil.isTrue(pickStockModel.getOuterId() == null, "领料人为空");
        AssertUtil.isTrue(pickStockModel.getOutCount() < 0, "出库数量不合理");
        AssertUtil.isTrue(pickStockModel.getOutCount() > projectBean.getBasicQuotationBean().getProductCount(), "出库数量不合理");
        InventoryBean inventoryBean = inventoryBeanMapper.selectByProjectNo(projectBean.getProjectNo());
        inventoryBean.setOutCount(pickStockModel.getOutCount());
        inventoryBean.setOutStatus(1);
        inventoryBean.setOuterId(pickStockModel.getOuterId());
        inventoryBean.setOutDate(pickStockModel.getOutDate());
        inventoryBeanMapper.updateByPrimaryKeySelective(inventoryBean);
    }
}